package com.shs.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author shs
 * @date 2022/8/18
 */
@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA(){
        log.info(Thread.currentThread().getName() +"\t" + "testA");
        return "========testA========";
    }


    @GetMapping("/testB")
    public String testB(){
        log.info(Thread.currentThread().getName() + "\t" + "TestB");
        return "========testB========";
    }
    @GetMapping("/testD")
    public String testD(){
        try {
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        log.info("TestD测试平均响应时间规则！");
        return "=====testD===========";
    }
    //测试热点Key
    @GetMapping("/testKey")
    @SentinelResource(value = "testKey",blockHandler = "deal_HotKey")
    public String testHotKye(@RequestParam(value = "p1" ,required = false) String p1,
                             @RequestParam(value = "p2" ,required = false) String p2){
        return "热点key访问成功！";
    }
    @GetMapping("/hotKey2")
    @SentinelResource(value = "hotKey2" ,blockHandler = "deal_HotKey")
    public String getHotKey(@RequestParam(value = "k1",required = false) String k1){
        return "访问了热点key处理方法！";
    }
    //处理热点key失败时候调用的方法
    public String deal_HotKey(String p1 , String p2, BlockException exception){
        return "调用了热点key处理方法失败的处理方法（失败）";

    }
}
